﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brain.Photon;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Brain.Photon
{
    /// <summary>
    /// Works on every platform. Only 2D
    /// </summary>
    public class PhotonSpriteBatchRenderer : PhotonRenderer
    {
        public SpriteBatch SpriteBatch;

        public float Distance
        {
            get { return distance; }
            set
            {
                distance = value;
                distanceUnder = 1 / value;
            }
        }
        private float distance = 1;

        private float distanceUnder = 1;
        
        /// <summary>
        /// Instantiates a new PhotonSpriteBatchRenderer
        /// </summary>
        /// <param name="system">The Particle Emitter you want this PhotonRenderer to draw</param>
        /// <param name="mBatch">The SpriteBatch to render the Particles with</param>
        public PhotonSpriteBatchRenderer(ParticleEmitter emitter, SpriteBatch mBatch)
            : base(emitter)
        {
            this.SpriteBatch = mBatch;
        }
        /// <summary>
        /// Instantiates a new PhotonSpriteBatchRenderer
        /// </summary>
        /// <param name="system">The Particle Emitter you want this PhotonRenderer to draw</param>
        public PhotonSpriteBatchRenderer(ParticleEmitter emitter)
            : base(emitter)
        {
            this.SpriteBatch = new SpriteBatch(Engine.Instance.GraphicsDevice);
            Load();
        }

        private void Load()
        {
            this.CanHandleParticleUpdate = true;
        }

        /// <inheritdoc />
        public override void Draw()
        {
            DrawParticleEmmiter(this.ParticleEmitter);

        }

        /// <summary>
        /// Draws a Particle Emmiter
        /// </summary>
        /// <param name="emitter">The emmiter to draw</param>
        protected virtual void DrawParticleEmmiter(ParticleEmitter emitter)
        {
            SpriteBatch.Begin();

            Vector2 origin = new Vector2(emitter.Properties.Texture.Width / 2, emitter.Properties.Texture.Height / 2);

            for (int i = emitter.MaxParticles - 1; i >= 0; i--)
            {
                Particle p = emitter.Particles[i];
                if (p != null)
                {
                    if (this.ShouldHandleParticleUpdate)
                    {
                        emitter.UpdateParticle(p, i);
                    }
                    
                    Vector2 position = new Vector2(p.Position.X, p.Position.Y);
                    Color color = emitter.Properties.StartColor;
                    float rotation = p.Rotation.X;
                    if (p.AliveTime > emitter.Properties.StartFadeOutTime)
                    {
                        float differenceOfAlive = p.AliveTime - emitter.Properties.MaxTime;
                        float dif =  emitter.Properties.DifferenceFromFadeOutToEnd;
                        float amount = (differenceOfAlive / (float)dif) + 1;
                        color = Color.Lerp(emitter.Properties.StartColor, emitter.Properties.EndColor, amount);
                    }
                    else if (p.AliveTime < emitter.Properties.TimeToDefaultColor)
                    {
                        float differenceOfAlive = emitter.Properties.TimeToDefaultColor - p.AliveTime;
                        float amount = ((emitter.Properties.TimeToDefaultColor - differenceOfAlive) / (float)emitter.Properties.TimeToDefaultColor);
                        color = Color.Lerp(Color.Transparent, emitter.Properties.StartColor, amount);
                    }
                    SpriteBatch.Draw(emitter.Properties.Texture, position, null, color, rotation, origin, p.Size, SpriteEffects.None, 0);
                }
            }

            SpriteBatch.End();
        }
    }
}
